import 'package:development_assistant/config/colors.dart';
import 'package:development_assistant/config/font.dart';
import 'package:development_assistant/controller/database_controller.dart';
import 'package:development_assistant/ui/screen/widget/custom_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class EditDatabase extends StatelessWidget {

  EditDatabase({super.key});

  final DatabaseController databaseController=Get.put(DatabaseController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _appBar(),
      bottomNavigationBar: _updateDatabase(),
      floatingActionButton: _delDatabase(),
      resizeToAvoidBottomInset: false,
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 20.w,horizontal: 20.w),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _databaseField("数据库名".tr,databaseController.dbName),
              _databaseField("数据库描述".tr,databaseController.dbDesc),
              _databaseField("主机号".tr, databaseController.dbHost),
              _databaseField("端口号".tr, databaseController.dbPort),
              _databaseField("Url",databaseController.dbUrl ),
            ],
          ),
        ),
      ),
    );
  }

  _delDatabase(){
    return Container(
      width: 50.w,
      height: 50.w,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(50),
          gradient: ColorsConfig().cardColorOfTheme
      ),
      child: Center(
        child: IconButton(
          onPressed: (){
            Get.defaultDialog(
              title: "删除数据库".tr,
              middleText: "${'是否删除该数据库?'.tr} : ${databaseController.dbName}",
              confirm: TextButton(
                style: TextButton.styleFrom(
                    backgroundColor:  ColorsConfig().buttonColorOfTheme,
                    foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10)
                    )
                ),
                onPressed: (){
                  databaseController.delDatabase();
                },
                child: Text("确定".tr),
              ),
              cancel:TextButton(
                style: TextButton.styleFrom(
                    backgroundColor:  ColorsConfig().buttonColorOfTheme,
                    foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10)
                    )
                ),
                onPressed: (){
                  Get.back();
                },
                child:Text("取消".tr),
              ),
            );
          },
          icon: const Icon(Icons.delete),
          color: Colors.black26,
        ),
      ),
    );
  }

  _updateDatabase(){
    return Container(
      width: 200.w,
      height: 35.h,
      color: Colors.white,
      margin: EdgeInsets.symmetric(vertical: 10.h),
      padding: EdgeInsets.symmetric(horizontal: 100.w),
      child: ElevatedButton(
        onPressed: (){
          Get.defaultDialog(
            title: "修改数据库".tr,
            middleText: "${'是否修改该数据库?'.tr} : ${databaseController.dbName}",
            confirm: TextButton(
              style: TextButton.styleFrom(
                  backgroundColor:  ColorsConfig().buttonColorOfTheme,
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)
                  )
              ),
              onPressed: (){
                databaseController.updateDatabase();
              },
              child: Text("确定".tr),
            ),
            cancel:TextButton(
              style: TextButton.styleFrom(
                  backgroundColor:  ColorsConfig().buttonColorOfTheme,
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)
                  )
              ),
              onPressed: (){
                Get.back();
              },
              child: Text("取消".tr),
            ),
          );
        },
        style: ElevatedButton.styleFrom(
            backgroundColor:  ColorsConfig().buttonColorOfTheme,
            foregroundColor: Colors.white,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(20)
            )
        ),
        child: Text("修改数据库".tr,style: TextStyle(fontWeight: FontWeight.bold),),
      ),
    );
  }

  _databaseField(String hintText,RxString dbValue){
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        CustomText(text: hintText, textStyle: FontConfig.fieldTitle),
        Container(
          width: 320.w,
          height: 50.h,
          margin: EdgeInsets.symmetric(vertical: 5.h),
          child: Center(
            child: TextField(
              controller: TextEditingController(text: dbValue.value),
              maxLines: 1,
              decoration: InputDecoration(
                  hintText:hintText,
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(15),
                  )
              ),
              onChanged: (e){
                dbValue.value=e;
              },
            ),
          ),
        ),
      ],
    );
  }

  _appBar(){
    return AppBar(
      title: Text("修改数据库".tr),
      centerTitle: true,
      leading: IconButton(
        onPressed: (){
          Get.offNamed("/Home",arguments: {"selectIndex":3});
        },
        icon: const Icon(Icons.arrow_back),
      ),
    );
  }
}